package bookstore_mall.resp;

import lombok.Data;

import java.io.Serializable;

@Data
public class ResponseResult<T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;


    /**
     * 返回信息
     */
    private String message;


    /**
     * 数据
     */
    private T data;

    public ResponseResult() {
        this.message = "";
        this.data = (T) new Object();
    }

    private ResponseResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    private ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    private ResponseResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * 全参数方法
     * @param code    状态码
     * @param message 返回信息
     * @param <T>     泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T>ResponseResult<T> response(Integer code, String message){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T>ResponseResult<T> response(Integer code, String message,T data){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        responseResult.setData(data);
        return responseResult;
    }


    /**
     * 成功返回（无参）
     *
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(T data,String message){
        return new ResponseResult<T>()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(message)
                .setData(data);
    }

    public static <T> ResponseResult<T> success(){
        return new ResponseResult<T>()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(ResultCodeEnum.SUCCESS.getMessage())
                .setData((T) new Object());
    }

    /**
     * 成功返回（无参）
     *
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(String message){
        return new ResponseResult<T>()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(message);
    }

    public static <T> ResponseResult<T> success(Integer code,String message){
        return new ResponseResult<T>()
                .setCode(code)
                .setMessage(message);
    }

    public static <T> ResponseResult<T> success(T data){
        return new ResponseResult<T>()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setData(data)
                .setMessage(ResultCodeEnum.getMessageByCode(ResultCodeEnum.SUCCESS.getCode()));
    }





    /**
     * 失败返回（无参）
     *
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(ResponseResult result,String message,T data) {
        return response(result.getCode(),  message, data);
    }

    public static <T> ResponseResult<T> fail(Integer code,String message,T data) {
        return response(code,  message, data);
    }

    public static <T> ResponseResult<T> fail(String message) {
        return new ResponseResult<T>()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setData((T) new Object())
                .setMessage(message);
    }



    /**
     * 失败返回（返回信息）
     *
     * @param message 返回信息
     * @param <T>  泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(ResultCodeEnum resultCodeEnum,String message) {
        return response(resultCodeEnum.getCode(),  message);
    }

    public static <T> ResponseResult<T> fail(Integer code,String message) {
        return response(code,  message);
    }


    //通用设置data
    public ResponseResult<T> data(T data){
        return this.setData(data);
    }
    //自定义状态信息
    public ResponseResult<T>  code(Integer code){
        return this.setCode(code);
    }
    // 自定义状态信息
    public ResponseResult<T> message(String message) {
        return this.setMessage(message);
    }
}
